package cn.itsource.config;

import cn.itsource.basic.util.SendUtil;
import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Map;

/**
 * @description: 消费者
 */
@Component
public class MessageConsumer {

    //发送短信
    @RabbitListener(queues = {"queue_sms"})
    public void handlerSMSMessage(String content, Message message, Channel channel) {
        System.out.println("发送短信：" + content);
        Map<String,String> map = JSON.parseObject(content, Map.class);
        //调用第三方接口，发送短信操作
        SendUtil.sendSMS(map.get("phone"),map.get("text"));
        //.......

        //手动签收消息
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("发送短信，签收失败：" + e);
        }
    }

    //发送邮件
    @RabbitListener(queues = {"queue_email"})
    public void handlerEmailMessage(String content, Message message, Channel channel) {
        System.out.println("发送邮件：" + content);
        //调用接口，发送短信操作
        //.......

        //手动签收消息
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("发送邮件，签收失败：" + e);
        }
    }

    //发送站内信
    @RabbitListener(queues = {"queue_station"})
    public void handlerStationMessage(String content, Message message, Channel channel) {
        System.out.println("发送站内信：" + content);
        //保存信息到数据库
        //.......

        //手动签收消息
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("发送站内信，签收失败：" + e);
        }
    }
}